package com.ww.word.ui.more;

import net.youmi.android.diy.DiyManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ww.core.activity.SlideMenuActivity;
import com.ww.core.util.CoreSharedPreferencesHelper;
import com.ww.core.util.DeviceUtil;
import com.ww.core.widget.dialog.AlertDialog;
import com.ww.core.widget.dialog.InputDialog;
import com.ww.core.widget.dialog.MsgDialog;
import com.ww.word.R;
import com.ww.word.db.StudyDbHelper;
import com.ww.word.ui.menu.LeftFragment;

/**
 * @since 
 * @version 2013-12-13
 * @author wangwei
 */
public class MoreActivity extends SlideMenuActivity implements OnClickListener {
	private LinearLayout share, appraise, feedback, version, backinit, about, num, skin, tuijian;
	private TextView num_txt;
	private String more_learn_count;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.activity_more, container, false);
		initView(view);
		return view;
	}

	private void initView(View view) {
		setTitle(view, LeftFragment.mTitleName);
		more_learn_count = getString(R.string.more_learn_count);
		share = (LinearLayout) view.findViewById(R.id.share);
		skin = (LinearLayout) view.findViewById(R.id.skin);
		appraise = (LinearLayout) view.findViewById(R.id.appraise);
		backinit = (LinearLayout) view.findViewById(R.id.backinit);
		num = (LinearLayout) view.findViewById(R.id.num);
		feedback = (LinearLayout) view.findViewById(R.id.feedback);
		version = (LinearLayout) view.findViewById(R.id.version);
		about = (LinearLayout) view.findViewById(R.id.about);
		tuijian = (LinearLayout) view.findViewById(R.id.tuijian);
		num_txt = (TextView) view.findViewById(R.id.num_txt);

		share.setOnClickListener(this);
		appraise.setOnClickListener(this);
		backinit.setOnClickListener(this);
		feedback.setOnClickListener(this);
		version.setOnClickListener(this);
		about.setOnClickListener(this);
		num.setOnClickListener(this);
		skin.setOnClickListener(this);
		tuijian.setOnClickListener(this);

		CoreSharedPreferencesHelper h = new CoreSharedPreferencesHelper(context);
		num_txt.setText(more_learn_count + h.getValue("num"));
		
		String num = h.getValue("num_tuijian");
		int count = 1;
		if (num != null) {
			count = Integer.parseInt(num);
		}
		h.setValue("num_tuijian", (count+1)+"");
		if (count < 5) {
			tuijian.setVisibility(View.GONE);
		} else {
			tuijian.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.share:
			DeviceUtil.share(context, getString(R.string.more_share), getString(R.string.more_share), null);
			break;
		case R.id.appraise:
			String str = "market://details?id=" + context.getPackageName();
			Intent localIntent = new Intent("android.intent.action.VIEW");
			localIntent.setData(Uri.parse(str));
			startActivity(localIntent);
			break;
		case R.id.feedback:
			startActivity(new Intent(context, FeedBackActivity.class));
			break;
		case R.id.version:
			break;
		case R.id.backinit:
			final AlertDialog a = new AlertDialog(context);
			a.show(getString(R.string.more_back), getString(R.string.more_back_sure), new OnClickListener() {
				@Override
				public void onClick(View v) {
					a.cancel();
					StudyDbHelper w = new StudyDbHelper(context);
					w.deleteStudy();
					MsgDialog.show(context, R.string.more_back_ok);
				}
			});
			break;
		case R.id.tuijian:
			DiyManager.showRecommendWall(context);
			break;
		case R.id.about:
			startActivityForResult(new Intent(context, AboutActivity.class), 1);
			break;
		case R.id.skin:
			startActivity(new Intent(context, SkinActivity.class));
			break;
		case R.id.num:
			final InputDialog inputDialog = new InputDialog(context);
			inputDialog.setType(android.text.InputType.TYPE_CLASS_NUMBER);
			final CoreSharedPreferencesHelper h = new CoreSharedPreferencesHelper(context);
			inputDialog.show(more_learn_count, null, h.getValue("num"), new OnClickListener() {
				@Override
				public void onClick(View v) {
					inputDialog.cancel();
					h.setValue("num", inputDialog.getText());
					num_txt.setText(more_learn_count + " " + inputDialog.getText());
				}
			});
			break;
		}
	}

}
